package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {

    @Autowired
    DtsStorageMapper storageMapper;

    @Value("${aliyun.oss.endpoint}")
    String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    String bucketName;

    @Override
    public IPage<DtsStorage> queryPage(Integer page, Integer limit, String key, String name, String sort, String order) {
        IPage<DtsStorage> ipage = new Page<>(page , limit);

        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.like("`key`", key);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name", name);
        }

        if(order.equalsIgnoreCase("asc")){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }


        return storageMapper.selectPage(ipage,wrapper );
    }

    @Override
    public DtsStorage insert(String ext, Integer size, byte[] content) throws Exception {

        //文件唯一的标识 fdsklfjaslkfjlkasdjflkasjdflksdjpg
        String uuid = UUID.randomUUID().toString();

        //上传图片到阿里云
        String url = AliyunOSSUtil.uploadImage(endpoint,accessKeyId, accessKeySecret, bucketName, uuid + ext ,content);

        DtsStorage storage = new DtsStorage();
        storage.setUpdateTime(new Date());
        storage.setAddTime(new Date());
        storage.setDeleted(false);
        storage.setSize(size);
        storage.setName(uuid + ext);
        storage.setKey(uuid);
        storage.setType(ext);
        storage.setUrl(url);

        storageMapper.insert(storage);
        return storage;
    }

//    @Override
//    public void update(DtsStorage storage) {
//        storageMapper.updateById(storage);
//    }



    @Override
    public DtsStorage findStorage(String url) {
        LambdaQueryWrapper<DtsStorage> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DtsStorage::getDeleted,false);
        wrapper.eq(DtsStorage::getUrl,url);

        DtsStorage dtsStorage = storageMapper.selectOne(wrapper);
        return dtsStorage;
    }

    @Override
    public void deleteById(DtsStorage storage) {

        if(storage.getName() != null){

            AliyunOSSUtil.deleteFile(endpoint,accessKeyId, accessKeySecret, bucketName,storage.getName());
            storageMapper.deleteById(storage);
        }

    }
    @Override
    public int update(DtsStorage storage) {
        int stor = storageMapper.updateById(storage);
        return stor;
    }

    @Override
    public int delete(DtsStorage storage) {
        String path = storage.getKey()+storage.getType();

        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, path);
        return storageMapper.deleteById(storage);

    }

    @Override
    public void deleteUrl(String fileName) {
        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
        wrapper.eq("url",fileName);
        DtsStorage storage = storageMapper.selectOne(wrapper);
        if(storage != null){
            if(storage.getUrl().equals(fileName)){
                String path = storage.getKey()+storage.getType();
                AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName,path);
                storageMapper.deleteById(storage);
            }
        }
    }
}
